import SwiftUI

struct SettingsView: View {
    // 设置项状态
    @AppStorage("showFileExtensions") private var showFileExtensions = true
    @AppStorage("sortOrder") private var sortOrder = SortOrder.nameAscending
    @AppStorage("defaultFileCreation") private var defaultFileCreation = "txt"
    @AppStorage("themeColor") private var themeColor = "blue"
    @State private var buildver = "1.0.0~beta 2"
    
    // 排序选项枚举
    enum SortOrder: String, CaseIterable, Identifiable {
        case nameAscending = "名称升序"
        case nameDescending = "名称降序"
        case dateAscending = "日期升序"
        case dateDescending = "日期降序"
        case sizeAscending = "大小升序"
        case sizeDescending = "大小降序"
        
        var id: Self { self }
    }
    
    // 主题颜色选项
    let themeColors = [
        "blue": "蓝色",
        "green": "绿色",
        "orange": "橙色",
        "purple": "紫色",
        "red": "红色"
    ]
    
    // 默认文件类型选项
    let fileTypes = ["txt", "md", "json", "csv", "xml"]
    
    var body: some View {
        NavigationView {
            Form {
                // 显示设置部分
                Section(header: Text("显示设置")) {
                    Toggle("显示文件扩展名", isOn: $showFileExtensions)
                    
                    Picker("排序方式", selection: $sortOrder) {
                        ForEach(SortOrder.allCases) { order in
                            Text(order.rawValue).tag(order)
                        }
                    }
                }
                
                // 文件操作设置部分
                Section(header: Text("文件操作")) {
                    Picker("默认文件类型", selection: $defaultFileCreation) {
                        ForEach(fileTypes, id: \.self) { type in
                            Text(".\(type)").tag(type)
                        }
                    }
                }
                
                // 外观设置部分
                Section(header: Text("外观设置")) {
                    Picker("主题颜色", selection: $themeColor) {
                        ForEach(themeColors.sorted(by: <), id: \.key) { key, value in
                            Text(value).tag(key)
                        }
                    }
                }
                
                // 关于部分
                Section {
                    NavigationLink(destination: AboutView()) {
                        Text("关于应用")
                    }
                    
                    Button("重置所有设置") {
                        resetSettings()
                    }
                    .foregroundColor(.red)
                }
            }
            .navigationTitle("设置")
            .navigationBarItems(trailing: Button("完成") {
                // 关闭设置视图的逻辑
            })
        }
    }
    
    // 重置设置函数
    private func resetSettings() {
        showFileExtensions = true
        sortOrder = .nameAscending
        defaultFileCreation = "txt"
        themeColor = "blue"
    }
}

// 关于视图
struct AboutView: View {
    var body: some View {
        VStack(spacing: 20) {
            Image(systemName: "folder.fill")
                .font(.system(size: 60))
                .foregroundColor(.blue)
            
            Text("SwiftFile")
                .font(.largeTitle)
                .bold()
            
            Text("版本1.0.0～beta 2")
                .foregroundColor(.gray)
            
            Text("一款简洁高效的文件管理工具")
                .multilineTextAlignment(.center)
                .padding()
            
            Spacer()
            
            Text("© 2025 @Infinity-Explorer")
                .font(.footnote)
                .foregroundColor(.secondary)
        }
        .padding()
        .navigationTitle("关于")
    }
}

// 预览
struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}


